package com.xu.controller;

import com.github.pagehelper.PageInfo;
import com.xu.pojo.College;
import com.xu.pojo.Course;
import com.xu.pojo.User;
import com.xu.pojo.UserDto;
import com.xu.service.CollegeService;
import com.xu.service.CourseService;
import com.xu.service.SelectService;
import com.xu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
public class TeacherController {

    @Autowired
    @Qualifier("collegeServiceImpl")
    private CollegeService collegeService;

    @Autowired
    @Qualifier("courseServiceImpl")
    private CourseService courseService;

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("selectServiceImpl")
    private SelectService selectService;

    private final int pageSize = 5;


    @RequestMapping("/admtea/queryCourse")
    public String queryCourse(Model model){
        PageInfo<Course> courses = courseService.queryCoursePaging(1, pageSize);
        List<College> colleges = collegeService.queryAllCollegeExceptOffice();
        model.addAttribute("courses",courses);
        model.addAttribute("colleges",colleges);
        return "tea/course";
    }


    @PostMapping("/admtea/queryCourseIf")
    @ResponseBody
    public PageInfo<Course> queryCourseIf(String queryCondition,int collegeId,int pageNum){
        PageInfo<Course> courses = courseService.queryCourseIfPaging(queryCondition, collegeId, pageNum, pageSize);
        return courses;
    }


    @RequestMapping("/tea/queryMyCourse")
    public String queryMyCourse(Model model){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        PageInfo<Course> courses = courseService.queryCourseByTeacherIdPaging(userDto.getUserId(),1,pageSize);
        model.addAttribute("courses",courses);
        if (selectService.isPrepareCourse())
            model.addAttribute("msg","课程信息已上传，请勿修改课程信息");
        return "tea/myCourse";
    }

    @PostMapping("/tea/queryMyCoursePaging")
    @ResponseBody
    public PageInfo<Course> queryMyCoursePaging(int pageNum){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        PageInfo<Course> courses = courseService.queryCourseByTeacherIdPaging(userDto.getUserId(),pageNum,pageSize);
        return courses;
    }



    @DeleteMapping("/tea/deleteCourse")
    @ResponseBody
    public String deleteCourse(int courseId){
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        if (!selectService.isPrepareCourse() && courseService.checkCourse(userDto.getUserId(),courseId) && userDto.getRoles().equals("teacher")) {
            courseService.deleteCourse(courseId);
            return "success";
        }
        else{
            return "false";
        }
    }

    @RequestMapping("/tea/updateCourse/{courseId}")
    public String toUpdateCourse(@PathVariable("courseId") int courseId, Model model){
        if (selectService.isPrepareCourse())
            return "redirect:/tea/queryMyCourse";
        Course course = courseService.queryCourseById(courseId);
        List<College> colleges = collegeService.queryAllCollegeExceptOffice();
        model.addAttribute("course",course);
        model.addAttribute("colleges",colleges);
        return "tea/updateCourse";
    }

    @PostMapping("/tea/update")
    public String updateCourse(Course course){
        //System.out.println(course);
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        if (!selectService.isPrepareCourse() && courseService.checkCourse(userDto.getUserId(),course.getCourseId()) && userDto.getRoles().equals("teacher")) {
            courseService.updateCourse(course);
        }
        return "redirect:/tea/queryMyCourse";
    }

    @RequestMapping("/tea/addCourse")
    public String toAddCourse(Model model){
        if (selectService.isPrepareCourse())
            return "redirect:/tea/queryMyCourse";
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        User user = userService.queryUserByID(userDto.getUserId());
        List<College> colleges = collegeService.queryAllCollegeExceptOffice();
        model.addAttribute("colleges",colleges);
        model.addAttribute("teaCollegeId",user.getCollege().getCollegeId());
        return "tea/addCourse";
    }

    @PostMapping ("/tea/addCourse")
    public String AddCourse(Course course){
        if (selectService.isPrepareCourse())
            return "redirect:/tea/queryMyCourse";
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto)subject.getPrincipal();
        course.setTeacherId(userDto.getUserId());
        courseService.addCourse(course);
        return "redirect:/tea/queryMyCourse";
    }
}
